const {exec} = require("child_process");
const iconv = require("iconv-lite");

class FileEntityError extends Error {

}

async function execute(command) {
    return new Promise((resolve, reject) => {
        exec(command, {encoding: 'binary'}, (error, stdout, stderr) => {
            function decode() {
                return iconv.decode(Buffer.from(stdout, 'binary'), 'gbk')
            }

            if (error || stderr) {
                if (error?.code === 1) {
                    resolve(decode())
                } else {
                    console.error(error);
                    reject({error})
                }
            } else {
                resolve(decode());
            }
        });
    });
}

module.exports = {
    FileEntityError,
    execute
}